package com.zzf.activitylifecycletest

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val tag = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d(tag,"onCreate")

        if (savedInstanceState != null){
            val tempData = savedInstanceState.getString("data_key")
            Log.d(tag,"tempData is $tempData")
        }

        startNormalActivity.setOnClickListener {
            val intent = Intent(this,NormalActivity::class.java)
            startActivity(intent)
        }

        startDialogActivity.setOnClickListener {
            val intent = Intent(this,DialogActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(tag,"on Start")
    }

    override fun onResume() {
        super.onResume()
        Log.d(tag,"on Resume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(tag,"on Pause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(tag,"on Stop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(tag,"on Destroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(tag,"on Restart")
    }

    //在Activity被回收之前回调用这个方法
    //在这个方法内部就可以保存数据
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        val tempData = "Something you just typed"
        outState.putString("data_key",tempData)
    }
}